package com.kensure.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import co.kensure.frame.ResultInfo;
import co.kensure.frame.ResultRowInfo;
import co.kensure.frame.ResultRowsInfo;
import co.kensure.http.RequestUtils;
import co.kensure.mem.NumberUtils;
import co.kensure.mem.PageInfo;

import com.alibaba.fastjson.JSONObject;
import com.kensure.mycom.config.service.MyConfigService;
import com.kensure.shike.user.model.SKLogin;
import com.kensure.shike.user.model.SKUser;
import com.kensure.shike.user.model.SKUserAddr;
import com.kensure.shike.user.model.SKUserSJYue;
import com.kensure.shike.user.model.SKUserShouYi;
import com.kensure.shike.user.model.SKUserVip;
import com.kensure.shike.user.model.query.SKUserListQuery;
import com.kensure.shike.user.service.SKLoginService;
import com.kensure.shike.user.service.SKSmsService;
import com.kensure.shike.user.service.SKUserAddrService;
import com.kensure.shike.user.service.SKUserFRService;
import com.kensure.shike.user.service.SKUserSJYueService;
import com.kensure.shike.user.service.SKUserService;
import com.kensure.shike.user.service.SKUserVipService;
import com.kensure.shike.weixin.service.WeixinXiaoChengXuService;
import com.kensure.shike.zhang.model.SKUserYue;
import com.kensure.shike.zhang.service.SKUserYueService;

/**
 * 用户的逻辑处理
 * 
 * @author fankaidi
 *
 */
@Controller
@RequestMapping(value = "user")
public class SKUserController {

	@Resource
	private SKUserService sKUserService;
	@Resource
	private SKSmsService sKSmsService;
	@Resource
	private MyConfigService myConfigService;
	@Resource
	private SKLoginService sKLoginService;
	@Resource
	private SKUserYueService sKUserYueService;
	@Resource
	private SKUserFRService sKUserFRService;
	@Resource
	private SKUserVipService sKUserVipService;
	@Resource
	private SKUserSJYueService sKUserSJYueService;
	@Resource
	private WeixinXiaoChengXuService weixinXiaoChengXuService;

	/**
	 * 验证码发送，包括试客、商家
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "sms.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo smsuser(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		String mobile = json.getString("mobile");
		int type = json.getInteger("type");
		sKSmsService.sendQRSms(mobile, type);
		return new ResultRowInfo();
	}

	/**
	 * 修改用户信息
	 */
	@ResponseBody
	@RequestMapping(value = "updateuser.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo updateuser(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		String phone = json.getString("phone");
		String topImg = json.getString("topImg");
		String nickname = json.getString("nickname");
		String realname = json.getString("realname");
		SKUser sk = sKUserService.getUser();
		sKUserService.updatePhone(sk.getId(), phone);
		sk.setPhone(phone);
		sk.setTopImg(topImg);
		sk.setRealname(realname);
		sk.setNickname(nickname);
		sKUserService.update(sk);
		return new ResultRowInfo();
	}

	/**
	 * 用户列表
	 */
	@ResponseBody
	@RequestMapping(value = "userlist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo userlist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		SKUserListQuery userQuery = JSONObject.parseObject(json.toJSONString(), SKUserListQuery.class);
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		List<SKUser> list = sKUserService.selectList(userQuery, page);
		long cont = sKUserService.selectListCount(userQuery);
		return new ResultRowsInfo(list, cont);
	}

	/**
	 * 用户大屏数量
	 */
	@ResponseBody
	@RequestMapping(value = "usercount.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo usercount(HttpServletRequest req, HttpServletResponse rep) {
		// JSONObject json = RequestUtils.paramToJson(req);
		long cont = sKUserService.selectListCount(null);
		cont = cont + NumberUtils.parseInteger(myConfigService.selectOne("zrenshu").getVal(), 0);
		return new ResultRowInfo(cont);
	}

	/**
	 * 设置靓号
	 */
	@ResponseBody
	@RequestMapping(value = "updateCode.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo updateCode(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		String code = json.getString("code");
		Long id = json.getLong("id");
		sKUserService.updateCode(id, code);
		return new ResultRowInfo();
	}

	// 开始
	@Resource
	private SKUserAddrService sKUserAddrService;

	/**
	 * 123 用户微信登录
	 */
	@ResponseBody
	@RequestMapping(value = "wxapp/login.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo login(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		String code = json.getString("code");
		String nickName = json.getString("nickName");
		String topImg = json.getString("topImg");
		Long refereeId = json.getLong("refereeId");
		String adb = weixinXiaoChengXuService.weixinLogin(code, nickName, topImg, refereeId);
		return new ResultRowInfo(adb);
	}

	/**
	 * 123 用户绑定手机
	 */
	@ResponseBody
	@RequestMapping(value = "wxapp/bindmobile.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo bindmobile(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		String encryptedData = json.getString("encryptedData");
		String iv = json.getString("iv");
		String code = json.getString("code");
		weixinXiaoChengXuService.weixinUpdatePhone(code, encryptedData, iv);
		return new ResultRowInfo();
	}

	/**
	 * 更新邀请人
	 */
	@ResponseBody
	@RequestMapping(value = "updateref.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo updateref(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long refereeId = json.getLong("refereeId");
		SKUser user = sKUserService.updateRefById(refereeId);
		return new ResultRowInfo(user);
	}

	/**
	 * 根据会话获取用户信息123
	 */
	@ResponseBody
	@RequestMapping(value = "getuser.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getuser(HttpServletRequest req, HttpServletResponse rep) {
		String token = req.getParameter("token");
		SKUser u = sKUserService.getUserByToken(token);
		return new ResultRowInfo(u);
	}

	/**
	 * 获取邀请人
	 */
	@ResponseBody
	@RequestMapping(value = "getrefuser.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getrefuser(HttpServletRequest req, HttpServletResponse rep) {
		String token = req.getParameter("token");
		SKUser u = sKUserService.getRefUserByToken(token);
		return new ResultRowInfo(u);
	}

	/**
	 * 根据邀请码获取信息
	 */
	@ResponseBody
	@RequestMapping(value = "getuserbycode.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getuserbycode(HttpServletRequest req, HttpServletResponse rep) {
		String code = req.getParameter("code");
		SKUser u = sKUserService.getUserByCode(code);
		return new ResultRowInfo(u);
	}

	/**
	 * 根据会话获取用户会员有效期
	 */
	@ResponseBody
	@RequestMapping(value = "getvip.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getvip(HttpServletRequest req, HttpServletResponse rep) {
		SKUserVip u = sKUserVipService.getUserVip();
		return new ResultRowInfo(u);
	}

	/**
	 * 根据会员获取的收益
	 */
	@ResponseBody
	@RequestMapping(value = "getshouyi.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getshouyi(HttpServletRequest req, HttpServletResponse rep) {
		SKUser user = sKUserService.getUser();
		SKUserShouYi u = sKUserFRService.getUserShouYi(user.getId());
		return new ResultRowInfo(u);
	}

	/**
	 * 用户地址列表
	 */
	@ResponseBody
	@RequestMapping(value = "useraddr.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo useraddr(HttpServletRequest req, HttpServletResponse rep) {
		SKUser user = sKUserService.getUser();
		List<SKUserAddr> addrlist = sKUserAddrService.selectByUserId(user.getId());
		return new ResultRowsInfo(addrlist);
	}

	/**
	 * 用户地址保存
	 */
	@ResponseBody
	@RequestMapping(value = "saveaddr.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo saveaddr(HttpServletRequest req, HttpServletResponse rep) {
		SKUser user = sKUserService.getUser();
		JSONObject json = RequestUtils.paramToJson(req);
		SKUserAddr addr = JSONObject.parseObject(json.toJSONString(), SKUserAddr.class);
		sKUserAddrService.save(user.getId(), addr);
		return new ResultInfo();
	}

	/**
	 * 地址详情
	 */
	@ResponseBody
	@RequestMapping(value = "getaddr.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getaddr(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		SKUserAddr addr = sKUserAddrService.selectOne(id);
		return new ResultRowInfo(addr);
	}

	/**
	 * 账户余额
	 */
	@ResponseBody
	@RequestMapping(value = "getzhyue.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getzhyue(HttpServletRequest req, HttpServletResponse rep) {
		SKUserYue u = sKUserYueService.selectByUser();
		return new ResultRowInfo(u);
	}

	/**
	 * 我的粉丝列表111
	 */
	@ResponseBody
	@RequestMapping(value = "fensilist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo fensilist(HttpServletRequest req, HttpServletResponse rep) {
		List<SKUser> list = sKUserService.getListByRefereeId();
		return new ResultRowsInfo(list);
	}

	/**
	 * 代金券列表,小程序用
	 */
	@ResponseBody
	@RequestMapping(value = "djqlist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo djqlist(HttpServletRequest req, HttpServletResponse rep) {
		SKUser user = sKUserService.getUser();
		List<SKUserSJYue> list = sKUserSJYueService.selectByUserid(user.getId());
		return new ResultRowsInfo(list);
	}

	/**
	 * 代金券列表webview用
	 */
	@ResponseBody
	@RequestMapping(value = "djqlist1.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo djqlist1(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		String tokenid = json.getString("tokenid");
		SKLogin login = sKLoginService.selectByTokenId(tokenid);
		List<SKUserSJYue> list = sKUserSJYueService.selectByUserid(login.getUserid());
		return new ResultRowsInfo(list);
	}

	/**
	 * 生成用户二维码，如果不存在，就生成
	 */
	@ResponseBody
	@RequestMapping(value = "qr.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo qr(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		weixinXiaoChengXuService.genMyCode(id);
		return new ResultRowInfo();
	}

	/**
	 * 用户数
	 */
	@ResponseBody
	@RequestMapping(value = "yhs.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo yhs(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Integer yhs = json.getInteger("yhs");
		Long cnt = sKUserService.selectCountByGroup(yhs);
		return new ResultRowInfo(cnt);
	}

}
